﻿/* * * * * * * * * * * * * * * *
 * SDL ToolBox                 *
 * Copyright (c) VosSoft 2009  *
 * * * * * * * * * * * * * * * *
 * file:    class texture      *
 * date:    2009-05-07         *
 * * * * * * * * * * * * * * * */

using System;
using System.Drawing;

namespace SDL_ToolBox
{
    /// <summary>
    /// Speichert einen Texturblock
    /// </summary>
    public class Texture
    {
        /// <summary>
        /// X-Position in der Map (in Blöcken)
        /// </summary>
        private int x;
        /// <summary>
        /// Y-Position in der Map (in Blöcken)
        /// </summary>
        private int y;
        /// <summary>
        /// Texturdatei (max 10 Zeichen)
        /// </summary>
        private char[] name;
        /// <summary>
        /// X-Offset in der Texturdatei (in Blöcken)
        /// </summary>
        private int offX;
        /// <summary>
        /// Y-Offset in der Texturdatei (in Blöcken)
        /// </summary>
        private int offY;
        /// <summary>
        /// Größe eines Texturblocks (in Pixel)
        /// </summary>
        private int size;
        /// <summary>
        /// Rot-Farbwert für den Colorkey
        /// </summary>
        private uint r;
        /// <summary>
        /// Grün-Farbwert für den Colorkey
        /// </summary>
        private uint g;
        /// <summary>
        /// Blau-Farbwert für den Colorkey
        /// </summary>
        private uint b;
        /// <summary>
        /// Bild des Texturblocks (nur intern)
        /// </summary>
        private Bitmap bmp;

        /// <summary>
        /// Standardkonstruktor
        /// </summary>
        public Texture()
        {
            x = 0;
            y = 0;
            name = new char[10];
            offX = 0;
            offY = 0;
            size = 0;
            r = 0;
            g = 0;
            b = 0;
            bmp = null;
        }

        /// <summary>
        /// X-Position in der Map (in Blöcken)
        /// </summary>
        public int X
        {
            get { return x; }
            set { x = value; }
        }

        /// <summary>
        /// Y-Position in der Map (in Blöcken)
        /// </summary>
        public int Y
        {
            get { return y; }
            set { y = value; }
        }

        /// <summary>
        /// Texturdatei (max 10 Zeichen)
        /// </summary>
        public char[] Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// X-Offset in der Texturdatei (in Blöcken)
        /// </summary>
        public int OffX
        {
            get { return offX; }
            set { offX = value; }
        }

        /// <summary>
        /// Y-Offset in der Texturdatei (in Blöcken)
        /// </summary>
        public int OffY
        {
            get { return offY; }
            set { offY = value; }
        }
        
        /// <summary>
        /// Größe eines Texturblocks (in Pixel)
        /// </summary>
        public int Size
        {
            get { return size; }
            set { size = value; }
        }

        /// <summary>
        /// Rot-Farbwert für den Colorkey
        /// </summary>
        public uint R
        {
            get { return r; }
            set { r = value; }
        }

        /// <summary>
        /// Grün-Farbwert für den Colorkey
        /// </summary>
        public uint G
        {
            get { return g; }
            set { g = value; }
        }

        /// <summary>
        /// Blau-Farbwert für den Colorkey
        /// </summary>
        public uint B
        {
            get { return b; }
            set { b = value; }
        }

        /// <summary>
        /// Bild des Texturblocks (nur intern)
        /// </summary>
        public Bitmap Bmp
        {
            get { return bmp; }
            set { bmp = value; }
        }

        /// <summary>
        /// 2D-Texturarray initialisieren
        /// </summary>
        /// <param name="num1">X-Wert</param>
        /// <param name="num2">Y-Wert</param>
        /// <returns>Gibt das initialisierte Texturenarray zurück</returns>
        public static Texture[,] NewArray(int num1, int num2)
        {
            Texture[,] t = new Texture[num1, num2];
            for (int j = 0; j < num2; j++)
            {
                for (int i = 0; i < num1; i++)
                {
                    t[i, j] = new Texture();
                    t[i, j].x = i;
                    t[i, j].y = j;
                }
            }
            return t;
        }
        
        /// <summary>
        /// Setzt die Texturattribute
        /// </summary>
        /// <param name="filename">Dateiname der Textur</param>
        /// <param name="tOffX">X-Offset in der Texturdatei (in Blöcken)</param>
        /// <param name="tOffY">Y-Offset in der Texturdatei (in Blöcken)</param>
        /// <param name="tSize">Größe eines Texturblocks (in Pixel)</param>
        public void Set(char[] filename, int tOffX, int tOffY, int tSize)
        {
            try
            {
                filename.CopyTo(name, 0);
                ToolBox.zeroToSpace(name);
                offX = tOffX;
                offY = tOffY;
                size = tSize;
            }
            catch (IndexOutOfRangeException)
            {
                throw new IndexOutOfRangeException("Der Dateiname \"" + new String(filename) + "\" ist zu lang.\nMaximal 9 Zeichen inkl. Endung.");
            }
        }

        /// <summary>
        /// Setzt die Farbe für den Colorkey
        /// </summary>
        /// <param name="red">Rot-Wert</param>
        /// <param name="green">Grün-Wert</param>
        /// <param name="blue">Blau-Wert</param>
        public void setRGB(uint red, uint green, uint blue)
        {
            r = red;
            g = green;
            b = blue;
        }

        /// <summary>
        /// Setzt die Farbe für den Colorkey
        /// </summary>
        /// <param name="argb">Alpha-Rot-Grün-Blau-Wert</param>
        public void setRGB(int argb)
        {
            Color c = Color.FromArgb(argb);
            r = c.R;
            g = c.G;
            b = c.B;
        }

        /// <summary>
        /// Gibt die Farbe als 32-bit ARGB-Wert zurück
        /// </summary>
        /// <returns>32-bit-ARGB-Wert</returns>
        public int getRGB()
        {
            return Color.FromArgb((int)r, (int)g, (int)b).ToArgb();
        }

        /// <summary>
        /// Kopiert eine Textur in eine andere
        /// </summary>
        /// <param name="tex">Textur</param>
        /// <param name="withXY">X- und Y-Wert mit kopieren</param>
        public void Copy(Texture tex, bool withXY)
        {
            if (withXY == true)
            {
                x = tex.x;
                y = tex.y;
            }
            name = tex.name;
            offX = tex.offX;
            offY = tex.offY;
            size = tex.size;
            r = tex.r;
            g = tex.g;
            b = tex.b;
            bmp = tex.bmp;
        }
    }
}
